﻿using System.Windows;

namespace GameEngine.Clients.Silverlight.View
{
    using GameEngine.Clients.Silverlight.View.Components;
    using GameEngine.Model;
    using GameEngine.View;
    using GameEngine.View.Components;

    public class SilverlightMapMediator : MapMediator<int, FrameworkElement>
    {
        public SilverlightMapMediator(IWorldDataProxy<int> worldProxy, IMapView<int, FrameworkElement> viewComponent)
            : base(viewComponent,worldProxy)
        {
          
        }

        public new MapUC ViewComponent
        {
            get { return (MapUC)base.ViewComponent; }
        }

        public Point GetMapScreenSize()
        {
            //todo kd o mapa é 0,0 d´+a merda
            Point lowestPoint = new Point(WorldProxy.Map.Size.X - 1, 0);
            lowestPoint = ViewComponent.TileMap.Transform(new Point(
                (/*proxy.CameraPosition.X*proxy.TileSize.X +*/ lowestPoint.X * SilverlightModelFacade.Instance.Game.TileSize.X + SilverlightModelFacade.Instance.Game.TileSize.X / 2),
                ((/*proxy.CameraPosition.Y*proxy.TileSize.Y*/lowestPoint.Y * SilverlightModelFacade.Instance.Game.TileSize.Y - SilverlightModelFacade.Instance.Game.TileSize.Y / 2))),
                WorldProxy.Map.GetTileHeight(lowestPoint).Value);

            Point highestPoint = new Point(0, WorldProxy.Map.Size.Y - 1);
            highestPoint = ViewComponent.TileMap.Transform(new Point(
                (/*proxy.CameraPosition.X*proxy.TileSize.X*/highestPoint.X * SilverlightModelFacade.Instance.Game.TileSize.X - SilverlightModelFacade.Instance.Game.TileSize.X / 2),
                ((/*proxy.CameraPosition.Y*proxy.TileSize.Y + */highestPoint.Y * SilverlightModelFacade.Instance.Game.TileSize.Y + SilverlightModelFacade.Instance.Game.TileSize.Y / 2))),
                WorldProxy.Map.GetTileHeight(highestPoint).Value);

            Point westPoint = new Point(0, 0);
            westPoint = ViewComponent.TileMap.Transform(new Point(
                (/*proxy.CameraPosition.X*proxy.TileSize.X*/westPoint.X * SilverlightModelFacade.Instance.Game.TileSize.X - SilverlightModelFacade.Instance.Game.TileSize.X / 2),
                ((/*proxy.CameraPosition.Y*proxy.TileSize.Y*/westPoint.Y * SilverlightModelFacade.Instance.Game.TileSize.Y - SilverlightModelFacade.Instance.Game.TileSize.Y / 2))),
                WorldProxy.Map.GetTileHeight(westPoint).Value);

            Point eastPoint = new Point(WorldProxy.Map.Size.X - 1, WorldProxy.Map.Size.Y - 1);
            eastPoint = ViewComponent.TileMap.Transform(new Point(
                (/*proxy.CameraPosition.X * proxy.TileSize.X +*/ eastPoint.X * SilverlightModelFacade.Instance.Game.TileSize.X + SilverlightModelFacade.Instance.Game.TileSize.X / 2),
                ((/*proxy.CameraPosition.Y * proxy.TileSize.Y +*/ eastPoint.Y * SilverlightModelFacade.Instance.Game.TileSize.Y + SilverlightModelFacade.Instance.Game.TileSize.Y / 2))),
                WorldProxy.Map.GetTileHeight(eastPoint).Value);

            return new Point(WorldProxy.Map.Scale * (eastPoint.X - westPoint.X), WorldProxy.Map.Scale * (-(highestPoint.Y - lowestPoint.Y)));
        }
        
    }
}
